package com.cqupt.tr.login.controller;

import com.cqupt.tr.bean.MonitorUser;
import com.cqupt.tr.login.mapper.LoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    private LoginMapper loginMapper;

    @PostMapping("/register")
    public String registerUser(@RequestBody Map<String,String> registerUser){
        MonitorUser user = new MonitorUser();
        user.setUsername(registerUser.get("username"));
        // 记得注册的时候把密码加密一下
        user.setPassword(bCryptPasswordEncoder.encode(registerUser.get("password")));
        user.setRole("ROLE_USER");
        user.setDepartment(registerUser.get("department"));
        int insert = loginMapper.insert(user);
        return String.valueOf(insert);
    }
}
